package com.gykfbxxh.imovie.custom.view.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

public class ScrollListView extends ListView {
	// com.liyun.myscrollview.MyListView
	private boolean isFastScroll;
	private boolean isTouch;
	private long lastTime;
	private int scrollY;
	private boolean scrollYTop;
	private int firstVisibleItem;
	
	public ScrollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setListener();
	}

	public ScrollListView(Context context, AttributeSet attrs) {

		super(context, attrs);
		setListener();
	}

	public ScrollListView(Context context) {
		super(context);
		setListener();
	}

	private void setListener() {
		postDelayed(new Runnable() {
			
			@Override
			public void run() {
				setOnScrollListener(new OnScrollListener() {

					@Override
					public void onScrollStateChanged(AbsListView view, int scrollState) {
						// TODO Auto-generated method stub
						if(scrollState == OnScrollListener.SCROLL_STATE_IDLE ){
							if(scrollYTop){
								scrollYTop = false;
								scrollY = 500;
							}
							if(firstVisibleItem <= 2){
								isFastScroll = false;
							}
						}
					}

					@Override
					public void onScroll(AbsListView view, int firstVisibleItem,
							int visibleItemCount, int totalItemCount) {
//						ScrollListView.this.firstVisibleItem = firstVisibleItem ;
//						if(firstVisibleItem == 0){
//							if(scrollYTop && isFastScroll){
//								float f = (250f/(System.currentTimeMillis()-lastTime));
//								scrollY = (int) f * getChildAt(0).getHeight();
//								if(scrollY > 3000){
//									scrollY = 3000;
//								}
//							}
//							if (getScrollYRange() <= 0) {
//								PairScrollView viewParent = (PairScrollView) getParent();
//								if(viewParent != null){
//									viewParent.requestDisallowInterceptTouchEvent(false);
//									Log.d("zhh", "-----scrollY---" + scrollY);
//									if(!isTouch){
//										if(scrollYTop){
//											viewParent.fling(-scrollY, scrollY);
//										} else {
//											viewParent.fling(-scrollY);
//										}
//									}
//									scrollY = 500;
//								}
//							}
//							if(scrollYTop){
//								scrollYTop = false;
//							}
//						} else if(firstVisibleItem == 1){
//							scrollYTop = true;
//							lastTime = System.currentTimeMillis();
//						} else {
//							if(firstVisibleItem >= 2){
//								isFastScroll = true;
//							}
//							scrollYTop = false;
//						}
					}
				});
			}
		}, 500);
		
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			isTouch = true;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			isTouch = false;
			break;
		}
		return super.onTouchEvent(ev);
	}
	@Override
	public void smoothScrollBy(int distance, int duration) {
		// TODO Auto-generated method stub
		super.smoothScrollBy(distance, duration);
	}

	private int getScrollYRange() {
		View c = getChildAt(0);
		if (c == null) {
			return 0;
		}
		int firstVisiblePosition = getFirstVisiblePosition();
		int top = c.getTop();
		return -top + firstVisiblePosition * c.getHeight();
	}
}
